import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";
import _isUndefined from "lodash/isUndefined";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _dec13, _class, _class2;

/**
 * SMSConfig - 短信配置
 * @date: 2018-8-1

 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { withRouter } from 'dva/router';
import { Bind } from 'lodash-decorators';
import { Header, Content } from 'components/Page';
import { Button as ButtonPermission } from 'components/Permission';
import intl from 'utils/intl';
import formatterCollections from 'utils/intl/formatterCollections';
import notification from 'utils/notification';
import { filterNullValueObject, getCurrentOrganizationId, isTenantRoleLevel, encryptPwd } from 'utils/utils';
import QueryForm from "./QueryForm";
import ListTable from "./ListTable";
import DetailModal from "./DetailModal";
/**
 * 短信账户数据展示
 * @extends {PureComponent} - React.PureComponent
 * @reactProps {Object} formValues - 查询表单值
 * @reactProps {Object} tableRecord - 表格中信息的一条记录
 * @reactProps {Boolean} isCreate - 是否为新建账户
 * @reactProps {Boolean} modalVisible - 模态框是否可见
 * @return React.element
 */

var SMSConfig = (_dec = connect(function (_ref) {
  var smsConfig = _ref.smsConfig,
      loading = _ref.loading;
  return {
    smsConfig: smsConfig,
    tenantId: getCurrentOrganizationId(),
    tenantRoleLevel: isTenantRoleLevel(),
    querySMSListLoading: loading.effects['smsConfig/fetchSMSList'],
    saving: loading.effects['smsConfig/createSMS'] || loading.effects['smsConfig/editSMS']
  };
}), _dec2 = formatterCollections({
  code: ['cmsg.smsConfig', 'cmsg.common']
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec13 = Bind(), _dec(_class = withRouter(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(SMSConfig, _PureComponent);

  var _super = _createSuper(SMSConfig);

  function SMSConfig() {
    var _this;

    _classCallCheck(this, SMSConfig);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.form = void 0;
    _this.state = {
      modalVisible: false,
      isCreate: true,
      tableRecord: {},
      isCopy: false
    };
    return _this;
  }

  _createClass(SMSConfig, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.fetchTableList();
      this.fetchServerTypeCode();
      this.fetchPublicKey();
    } // 获取短信列表信息

  }, {
    key: "fetchTableList",
    value: function fetchTableList() {
      var fields = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dispatch = this.props.dispatch;
      var fieldValues = _isUndefined(this.form) ? {} : filterNullValueObject(this.form.getFieldsValue());
      dispatch({
        type: 'smsConfig/fetchSMSList',
        payload: _objectSpread({
          page: _isEmpty(fields) ? {} : fields
        }, fieldValues)
      });
    } // 获取服务类型

  }, {
    key: "fetchServerTypeCode",
    value: function fetchServerTypeCode() {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'smsConfig/fetchServerType'
      });
    } // 打开新增模态框

  }, {
    key: "showModal",
    value: function showModal() {
      this.setState({
        modalVisible: true,
        isCreate: true,
        isCopy: false
      });
    } // 打开编辑模态框

  }, {
    key: "showEditModal",
    value: function showEditModal() {
      this.setState({
        modalVisible: true,
        isCreate: false,
        isCopy: false
      });
    } // 关闭模态框

  }, {
    key: "handleCancel",
    value: function handleCancel() {
      this.setState({
        modalVisible: false,
        isCreate: true,
        tableRecord: {},
        isCopy: false
      });
    }
    /**
     * 请求公钥
     */

  }, {
    key: "fetchPublicKey",
    value: function fetchPublicKey() {
      var _this$props$dispatch = this.props.dispatch,
          dispatch = _this$props$dispatch === void 0 ? function () {} : _this$props$dispatch;
      dispatch({
        type: 'smsConfig/getPublicKey'
      });
    } // 新建短信账户

  }, {
    key: "handleAdd",
    value: function handleAdd(values) {
      var _this2 = this;

      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          _this$props$smsConfig = _this$props.smsConfig,
          _this$props$smsConfig2 = _this$props$smsConfig.pagination,
          pagination = _this$props$smsConfig2 === void 0 ? {} : _this$props$smsConfig2,
          publicKey = _this$props$smsConfig.publicKey,
          tenantId = _this$props.tenantId,
          tenantRoleLevel = _this$props.tenantRoleLevel;

      var newValues = _objectSpread({}, values);

      if (values.accessKeySecret) {
        newValues.accessKeySecret = encryptPwd(values.accessKeySecret, publicKey);
      }

      var params = _objectSpread(_objectSpread({}, newValues), {}, {
        tenantId: tenantRoleLevel ? tenantId : values.tenantId
      });

      dispatch({
        type: 'smsConfig/createSMS',
        payload: params
      }).then(function (response) {
        if (response) {
          _this2.handleCancel();

          _this2.fetchTableList(pagination);

          notification.success();
        }
      });
    } // 编辑短信账户

  }, {
    key: "handleEdit",
    value: function handleEdit(values) {
      var _this3 = this;

      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          _this$props2$smsConfi = _this$props2.smsConfig,
          _this$props2$smsConfi2 = _this$props2$smsConfi.pagination,
          pagination = _this$props2$smsConfi2 === void 0 ? {} : _this$props2$smsConfi2,
          publicKey = _this$props2$smsConfi.publicKey,
          tenantId = _this$props2.tenantId,
          tenantRoleLevel = _this$props2.tenantRoleLevel;

      var newValues = _objectSpread({}, values);

      if (values.accessKeySecret) {
        newValues.accessKeySecret = encryptPwd(values.accessKeySecret, publicKey);
      }

      var editParams = _objectSpread(_objectSpread({}, newValues), {}, {
        tenantId: tenantRoleLevel ? tenantId : values.tenantId
      });

      dispatch({
        type: 'smsConfig/editSMS',
        payload: editParams
      }).then(function (response) {
        if (response) {
          _this3.handleCancel();

          _this3.fetchTableList(pagination);

          notification.success();
        }
      });
    } // 获取编辑数据记录

  }, {
    key: "getRecordData",
    value: function getRecordData(record) {
      this.setState({
        tableRecord: _objectSpread({}, record)
      });
      this.showEditModal();
    }
  }, {
    key: "handleCopy",
    value: function handleCopy(record) {
      this.setState({
        tableRecord: record,
        isCopy: true,
        modalVisible: true
      });
    }
  }, {
    key: "handleDelete",
    value: function handleDelete(record) {
      var _this4 = this;

      var dispatch = this.props.dispatch;
      dispatch({
        type: 'smsConfig/deleteSMS',
        payload: record
      }).then(function (res) {
        if (res) {
          notification.success();

          _this4.fetchTableList();
        }
      });
    }
    /**
     * 设置Form
     * @param {object} ref - FilterForm组件引用
     */

  }, {
    key: "handleBindRef",
    value: function handleBindRef() {
      var ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.form = (ref.props || {}).form;
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          _this$props3$smsConfi = _this$props3.smsConfig,
          _this$props3$smsConfi2 = _this$props3$smsConfi.smsData,
          smsData = _this$props3$smsConfi2 === void 0 ? {} : _this$props3$smsConfi2,
          _this$props3$smsConfi3 = _this$props3$smsConfi.serverTypeList,
          serverTypeList = _this$props3$smsConfi3 === void 0 ? [] : _this$props3$smsConfi3,
          _this$props3$smsConfi4 = _this$props3$smsConfi.pagination,
          pagination = _this$props3$smsConfi4 === void 0 ? {} : _this$props3$smsConfi4,
          querySMSListLoading = _this$props3.querySMSListLoading,
          saving = _this$props3.saving,
          tenantId = _this$props3.tenantId,
          tenantRoleLevel = _this$props3.tenantRoleLevel,
          path = _this$props3.match.path;
      var _this$state = this.state,
          modalVisible = _this$state.modalVisible,
          _this$state$tableReco = _this$state.tableRecord,
          tableRecord = _this$state$tableReco === void 0 ? {} : _this$state$tableReco,
          isCreate = _this$state.isCreate,
          isCopy = _this$state.isCopy;
      var formProps = {
        serverTypeList: serverTypeList,
        tenantRoleLevel: tenantRoleLevel,
        onSearch: this.fetchTableList,
        onRef: this.handleBindRef
      };
      var tableProps = {
        path: path,
        pagination: pagination,
        smsData: smsData,
        tenantId: tenantId,
        tenantRoleLevel: tenantRoleLevel,
        loading: querySMSListLoading,
        onGetRecord: this.getRecordData,
        onCopy: this.handleCopy,
        onChange: this.fetchTableList,
        onDelete: this.handleDelete
      };
      var detailProps = {
        isCopy: isCopy,
        title: isCreate ? intl.get('cmsg.smsConfig.view.message.create').d('新建短信账户') : intl.get('cmsg.smsConfig.view.message.edit').d('编辑短信账户'),
        modalVisible: modalVisible,
        serverTypeList: serverTypeList,
        tableRecord: tableRecord,
        isCreate: isCreate,
        saving: saving,
        tenantRoleLevel: tenantRoleLevel,
        onCancel: this.handleCancel,
        anchor: 'right',
        onAdd: this.handleAdd,
        onEdit: this.handleEdit
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cmsg.smsConfig.view.message.title.smsConfig').d('短信配置')
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.create"),
          type: 'button',
          meaning: '短信配置-新建'
        }],
        type: "primary",
        onClick: this.showModal,
        icon: "plus"
      }, intl.get('cpaas.common.button.create').d('新建'))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, /*#__PURE__*/React.createElement(QueryForm, formProps)), /*#__PURE__*/React.createElement(ListTable, tableProps)), /*#__PURE__*/React.createElement(DetailModal, detailProps));
    }
  }]);

  return SMSConfig;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "fetchTableList", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchTableList"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "showModal", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "showModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "showEditModal", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "showEditModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCancel", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCancel"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "fetchPublicKey", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchPublicKey"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleAdd", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleAdd"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleEdit", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleEdit"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "getRecordData", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "getRecordData"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCopy", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCopy"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDelete", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDelete"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleBindRef", [_dec13], Object.getOwnPropertyDescriptor(_class2.prototype, "handleBindRef"), _class2.prototype)), _class2)) || _class) || _class) || _class);
export { SMSConfig as default };